package com.yequan.thread.keyword.volatileword;

/**
 * @author : Administrator
 * @date : 2020/5/4
 */
public class MutableInteger {

    private volatile int value;

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public static void main(String[] args) {
        MutableInteger mutableInteger = new MutableInteger();
        for (int i = 0; i < 100; i++) {
            Thread thread1 = new Thread(() -> {
                mutableInteger.setValue(2);
                System.out.println(Thread.currentThread().getName() + "设置value为 2");
            });
            Thread thread2 = new Thread(() -> {
                int value = mutableInteger.getValue();
                System.out.println(Thread.currentThread().getName() + "获取value为 " + value);
            });
            thread1.start();
            thread2.start();
        }

    }

}
